<?php
class CmsManager extends CApplicationComponent
{
    /**
    * running site
    * @var array attributes of the Site model
    */
    protected $site = null;
    
    /**
    * running workflow
    * @var array attributes of the Workflow model
    */
    protected $workflow = null;
    
    // requested page
    protected $page;
    
    // theme
    protected $theme;
    
    public function init(){
        if (! @class_exists('Settings')){
            Yii::trace('Settings file is not found! File is generated into runtime/cache folder. Make sure it is writable. ');
            FSM::run('Core.Settings.db2php', array('module'=>'Core'));
        }        
    }
        
    public function getWorkflow(){ 
        return $this->workflow; 
    }
    
    public function setWorkflow($value){
        $this->workflow = $value;
        // update site details
        $this->getSite();
    }
    
    public function setSite($value){
        $this->site = $value;
        if ($this->site == null)
            $this->site = Site::model()->findByPk(FSM::run('Cms.Site.getDefault')->id)->attributes;
        // set the site theme base on it's hostname
        $this->setCmsTheme();
    }
    
    public function getSite(){
        if ($this->site == null )
        {
            $siteId = FSM::run('Cms.Site.getDefault')->id;
            $urlManager = Yii::app()->getComponent('urlManager');
            Yii::trace('urlFormat '.$urlManager->urlFormat, 'CmsManager');
            //TODO: fix bug detect correct themes by site
            //quick fix detect request GET and $_GET['layout'] != '' => backend, PATH => frontend
            //if frontend always get site_id by workflow
            if ($siteId <= 0 || $urlManager->urlFormat == CUrlManager::PATH_FORMAT || ($urlManager->urlFormat == CUrlManager::GET_FORMAT && isset($_GET['layout']) && $_GET['layout'] === 'popup'))
                $siteId = $this->workflow['site_id'];
            //TODO: what about ajax request
            $site = Site::model()->findByPk($siteId);
            if (is_object($site))
                $this->setSite($site->attributes);
            else
                Yii::log('SiteId: '.$siteId, Clogger::LEVEL_ERROR);
        }
            
        return $this->site;
    }
    
    /**
    * Set the front-end theme to use site's theme
    * For a site that use Cms module, all CMS pages use this theme so it's supposed to be
    * the application theme for other controller/action.
    */
    protected function setCmsTheme(){
        if (file_exists(Yii::app()->themeManager->basePath.'/'.Settings::THEME)){
//            if (Yii::app()->controller->isBackend == false)
//                Yii::app()->theme = $this->site['name'];        
            $this->theme = Yii::app()->theme;
        } else {
            $errorMsg = 'Folder '.Settings::THEME.' in application/themes not found. Theme folder does not exist for site '.Settings::THEME;
            FErrorHandler::logError(new FException($errorMsg, FException::DEVELOPER_EXCEPTION));
            
//            if (Yii::app()->controller->isBackend)
//                FErrorHandler::logError(new FException($errorMsg, FException::DEVELOPER_EXCEPTION));
//            else
//                throw new CHttpException(500, $errorMsg);
        }
    }
    
    /**
    * CMS theme
    * @return CTheme
    */
    public function getCmsTheme(){
        if ($this->theme == null) {
//            $name = $this->Site['name'];
            $basePath = Yii::app()->themeManager->basePath.'/'.Settings::THEME;
            $baseUrl =  str_replace(DIRECTORY_SEPARATOR,'/', baseUrl().'/sites/'.SITE_DIR.'/themes/'.Settings::THEME);
//            $this->theme = new CTheme($name, $basePath, $baseUrl);
            $this->theme = new CTheme(Settings::THEME, $basePath, $baseUrl);
        }
        return $this->theme;
    }
}
?>
